<?php 

class mondat_ctr extends CI_Controller {  
       
	    function __construct()
    {
        parent::__construct();
        $this->load->library("pagination");
		$this->load->helper(array('form', 'url'));
		$this->load->library('image_lib');
		$this->load->helper("file");
        $this->load->helper('form');
    }
	  
	 function index()
	{
		
				
		 $this->load->library('session');
		$this->load->model('charticle_mdl'); // load the file welcome_mdl.php
		$this->load->model('mondat_mdl'); // load the file welcome_mdl.php
		$charticle = $this->charticle_mdl->get_all(); // call the method filtera inside the class welcome_mdl
	    $data_charticle = array('data_charticle'=>$charticle); //ajouter la table r1 a data		
	   $this->session->set_userdata($data_charticle); // mettre $data_charticle dans une var globale
	   
	    $config = array();
          $config["base_url"] = base_url( "mondat_ctr/index");
		  $config["total_rows"] =$this->mondat_mdl->record_count_all();
		  $config["per_page"] = 20;
          $config["uri_segment"] = 3;
 
        $this->pagination->initialize($config);
		 $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
       $data["results"] = $this->mondat_mdl->get_all($config["per_page"], $page);
	   $data["results_recherche"] = $this->mondat_mdl->get_all_recherche();
        $data["links"] = $this->pagination->create_links();
		  $this->load->view('mondat_vew',$data);  // send the result from the model to the view 
	
	}
	
	
	public function find_mondat_per_fournis()
    {
		$this->load->model('mondat_mdl'); // load the file welcome_mdl.php
		$v=$this->input->post('nom');
	   
	   
	    $config = array();
          $config["base_url"] = base_url( "mondat_ctr/find_mondat_per_fournis");
		  $config["total_rows"] =$this->mondat_mdl->record_count_recherche($v);
		  $config["per_page"] = 20;
          $config["uri_segment"] = 3;
		  
		   $this->pagination->initialize($config);
		 $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
      
	    $data["results_recherche"] = $this->mondat_mdl->get_all_recherche();//elle appaort tt les fournisseur pour rechercher leur convention
        $data["links"] = $this->pagination->create_links();
	   $data["results"]= $this->mondat_mdl->get_recherche_per_nom($v);
	    $this->load->view("mondat_vew",$data);	
	   
    }
	
		public function get_mondat_per_article2($x1)
    {
		$this->load->model('mondat_mdl'); // load the file welcome_mdl.php
	 $this->load->library('session');
	  $tab['val']=$x1;
	   $tab['premier_appelle']=TRUE;
	   $tab['nbr-enreg']= $this->mondat_mdl->record_count($x1);
	  $this->session->set_userdata($tab); // mettre $tab dans une var globale
	  
	  

		
		$this->get_mondat_per_article3();
		
	
	
    }
	
	public function get_mondat_per_article3()
    {
		
	 $this->load->library('session');
        $x1=$this->session->userdata('val'); //access la var globale val

		$this->load->model('mondat_mdl'); // load the file welcome_mdl.php
		
			 /////pagination
		   $config = array();
         $config["base_url"] = base_url("mondat_ctr/get_mondat_per_article3");
		  $config["total_rows"] =$this->session->userdata('nbr-enreg');
		  $config["per_page"] = 20;
          $config["uri_segment"] = 3;
 //
       $this->pagination->initialize($config);
		  $premier=$this->session->userdata('premier_appelle'); //access la var globale val
	 if($premier){
	 	$page=0;
		 $tab['premier_appelle']=FALSE;
		   $this->session->set_userdata($tab); // mettre $tab dans une var globale
		   }
	else	 $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		 
		
       $data["results"] = $this->mondat_mdl->get_all2($config["per_page"], $page ,$x1);
	    $data["results_recherche"] = $this->mondat_mdl->get_recherche_per_art($x1);
        $data["links"] = $this->pagination->create_links();
		
		 $this->load->view("mondat_vew",$data);	
		
		
		
	
	
    }
	
	
	public function details_mondat($id_modat)
    {
		
			$this->load->model('mondat_mdl'); // load the file welcome_mdl.php
		    $this->load->model('fournisseur_mdl2');
		 
		    $data['fournisseurs'] = $this->fournisseur_mdl2->get_all();   // get all  for autocomplete
			
       $data["results"] = $this->mondat_mdl->get_details($id_modat);
	
		
		 $this->load->view("mondat_details_vew",$data);	
		
		
		
	
	
    }
    
	public function imprimer_mondat($id_modat)
    {
		
			$this->load->model('mondat_mdl'); // load the file welcome_mdl.php
		    $this->load->model('fournisseur_mdl2');
		 
		    $data['fournisseurs'] = $this->fournisseur_mdl2->get_all();   // get all  for autocomplete
			
       $data["results"] = $this->mondat_mdl->get_details($id_modat);
	
		
		 $this->load->view("mondat_imprimer_vew",$data);	
		
		
		
	
	
    }
    
	
	public function accord_cf($id)
	{
	
		
		  $this->load->model('bc_mdl');	
		  	$data['id_mandat']=$id; 
		 $this->load->view("mondat_accord_cf_vew",$data);	
	}
	
	public function valider_accord_cf($id)
	{
	
		
		  $this->load->model('mondat_mdl');	
		  
		  $date= $this->input->post('date');
		  $visa_cf= $this->input->post('visa_cf');
		   
		   $this->mondat_mdl->add_visa_cf($id,$visa_cf,$date);
		 echo" <script> alert (\"  تمت العملية بنجاح \") </script>" ;
		 
		  $this->details_mondat($id);
	}

   
	
	public function valider_accord_comptable($id)
	{
	
		
		  $this->load->model('mondat_mdl');	
		  
		  $date= $this->input->post('date');
		  $visa_comp= $this->input->post('visa_comptable');
		  $cheque= $this->input->post('num_cheque');
		  $ligne= $this->input->post('ligne');
		  $page= $this->input->post('page');
		 
		   
		   $this->mondat_mdl->add_visa_comptable($id,$visa_comp,$date,$page,$ligne,$cheque);
		 echo" <script> alert (\"  تمت العملية بنجاح \") </script>" ;
		 
		  $this->details_mondat($id);
	}
	
	public function update($id)
	{
	
		// check for data concistency
		$fournisseur= $this->input->post('fournisseur');
		$bc= $this->input->post('bc');
		$facture= $this->input->post('facture');
		$montant= $this->input->post('montant');
		$date= $this->input->post('date'); 
		$cheque = $this->input->post('cheque'); 
		
		// verifie si num fact existe  et le bc
		
		  $this->load->model('facture_mdl');
		  $this->load->model('bc_mdl');	
		  	 
		  $data = $this->facture_mdl->verifie_id($facture);   // get all  for autocomplete
		  $data2= $this->bc_mdl->get_detaille($bc);
		if($data and $data2){ // num facture existe et bc
			 $this->load->model('mondat_mdl');
			 $this->mondat_mdl->update_mondat($id,$facture,$montant,$cheque,$bc);
			}
	    else { // numero de facture ne existe pas
			echo" <script> alert (\" لا توجد فاتورة بهذا الرقم  \") </script>" ;
			 $this->details_mondat($id);
			 }
		$vv=1; 
	}

   
     
    public function ajout(){
		$this->load->model('fournisseur_mdl2');
		
		$liste_fourn = $this->fournisseur_mdl2->get_all();   // get all  for autocomplete
		
		$r['liste_fourn']    = $liste_fourn;
		
		
		$this->load->view('mondat_ajout_vew',$r); 
	}
	
	public function valider_ajout(){
		$fournisseur= $this->input->post('fournisseur');
		$bc= $this->input->post('bc');
		$facture= $this->input->post('facture');
		$montant= $this->input->post('montant');
		$date= $this->input->post('date'); 
		$cheque = $this->input->post('cheque'); 
		
		// verifie si num fact existe  et le bc
		
		  $this->load->model('facture_mdl');
		  $this->load->model('bc_mdl');	
		  	 
		  $data = $this->facture_mdl->verifie_id($facture);   // get all  for autocomplete
		  $data2= $this->bc_mdl->get_detaille($bc);
		if($data and $data2){ // num facture existe et bc
		     $today = getdate();
				$date=$today['year'].'-'.$today['mon'].'-'.$today['mday'];
			 $this->load->model('mondat_mdl');
			 $this->mondat_mdl->ajout_mondat($facture,$montant,$cheque,$bc,$date);
			 echo" <script> alert (\"  تمت العملية بنجاح \") </script>" ;
			  $this->index();
			}
	    else { // numero de facture ne existe pas
			echo" <script> alert (\" لا توجد فاتورة بهذا الرقم  \") </script>" ;
			   $this->ajout();
			 }
		$vv=1; 
	}
}
